<template>
  <div>
    <el-breadcrumb>
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品分类</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <el-row>
        <el-col>
          <el-button @click="showAddDialog" type="primary">添加分类</el-button>
        </el-col>
      </el-row>
      <zk-table :selection-type='false' :expand-type='false' ref="tableTable" index-text="#" :data="TableData" :columns="Tablecolumns" border show-index>
        <template slot-scope="scope" slot="isOK">
          <i v-if=" scope.row.cat_deleted ===false " class=" igreen   el-icon-circle-check"></i>
          <i v-else class=" ired   el-icon-circle-close"></i>
        </template>
        <template slot="sort" slot-scope="scope">
          <el-tag v-if=" scope.row.cat_level===0">一级</el-tag>
          <el-tag v-else-if='scope.row.cat_level===1' type="success ">二级</el-tag>
          <el-tag v-else type="warning">三级</el-tag>
        </template>
        <template slot="opt" slot-scope="scope">
          <el-button @click="showeditDialog(scope.row.cat_id)" size="mini" type="primary" icon="el-icon-edit">编辑</el-button>
          <el-button @click="removeById(scope.row.cat_id)" size="mini" type="danger" icon="el-icon-delete">删除</el-button>
        </template>
      </zk-table>
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="queryInfo.pagenum" :page-sizes="[1, 3, 10, 20]" :page-size="queryInfo.pagesize" layout="total, sizes, prev, pager, next, jumper" :total="total">
      </el-pagination>
    </el-card>
    <!-- 添加商品分类 -->
    <el-dialog title="添加分类" @close="addDialogClose" :visible.sync="addDialogVisible" width="50%">
      <el-form :model="addForm" :rules="addFormRules" ref="addFormRef" label-width="100px">
        <el-form-item label="分类名称" prop="cat_name">
          <el-input v-model="addForm.cat_name"></el-input>
        </el-form-item>
        <el-form-item label="父级分类">
          <el-cascader v-model="selectdKeys" :options="CateOptions" :props="CateProps" @change="handleChange"></el-cascader>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addSubmitBtn">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 修改商品分类 -->
    <el-dialog title="修改分类名称" :visible.sync="editDialogVisible" width="50%">
      <el-form :model="editForm" :rules="editFormRules" ref="editFormRef" label-width="100px">
        <el-form-item label="分类名称" prop="cat_name">
          <el-input v-model="editForm.cat_name"></el-input>
        </el-form-item>

      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="editSubmitBtn">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      Tablecolumns: [
        {
          label: '分类名称',
          prop: 'cat_name'
        },
        {
          label: '是否有效',
          type: 'template',
          template: 'isOK'
        },
        {
          label: '排序',
          type: 'template',
          template: 'sort'
        },
        {
          label: '操作',
          type: 'template',
          template: 'opt'
        }
      ],
      TableData: [],
      queryInfo: {
        type: '3',
        pagesize: 3,
        pagenum: 1
      },
      total: 0,
      addDialogVisible: false,
      addForm: {
        cat_name: ''
      },
      addFormRules: {
        cat_name: [{ required: true, message: '请输入分类名称', trigger: 'blur' }]
      },
      selectdKeys: [],
      CateOptions: [],
      CateProps: {
        label: 'cat_name',
        value: 'cat_id',
        children: 'children',
        checkStrictly: true
      },
      catID: 0,
      catLevel: 0,
      editDialogVisible: false,
      editForm: {
        cat_name: ''
      },
      editFormRules: {
        cat_name: [{ required: true, message: '请输入分类名称', trigger: 'blur' }]
      }
    }
  },
  created() {
    this.gettableData()
  },
  methods: {
    async gettableData() {
      const { data: res } = await this.$http.get('categories', {
        params: this.queryInfo
      })
      if (res.meta.status !== 200) {
        return this.$message.error('获取商品分类失败！ ')
      }
      this.TableData = res.data.result
      this.total = res.data.total
    },
    handleCurrentChange(page) {
      this.queryInfo.pagenum = page
      this.gettableData()
    },
    handleSizeChange(size) {
      this.queryInfo.pagesize = size
      this.gettableData()
    },

    async removeById(id) {
      const ConfirmResult = await this.$confirm('此操作将永久删除该商品分类, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => err)
      if (ConfirmResult !== 'confirm') {
        return this.$message.info('已取消删除')
      }
      const { data: res } = await this.$http.delete('categories/' + id)
      if (res.meta.status !== 200) {
        return this.$message.error('删除失败!')
      }
      this.$message.success('删除成功!')
      this.gettableData()
    },
    async showAddDialog() {
      const { data: res } = await this.$http.get('categories')
      if (res.meta.status !== 200) {
        return this.$message.error('获取商品分类失败！ ')
      }
      this.CateOptions = res.data
      this.addDialogVisible = true
    },
    addDialogClose() {
      console.log(111)
      this.$refs.addFormRef.resetFields()
      this.selectdKeys = []
      this.catID = 0
      this.catLevel = 0
    },
    handleChange() {
      if (this.selectdKeys.length === 0) {
        this.catID = 0
        this.catLevel = 0
        this.selectdKeys = []
      } else {
        this.catID = this.selectdKeys[this.selectdKeys.length - 1]
        this.catLevel = this.selectdKeys.length
      }
    },
    addSubmitBtn() {
      this.$refs.addFormRef.validate(async valid => {
        if (!valid) return
        const { data: res } = await this.$http.post('categories', {
          cat_pid: this.catID,
          cat_name: this.addForm.cat_name,
          cat_level: this.catLevel
        })
        if (res.meta.status !== 201) {
          return this.$message.error('添加分类失败!')
        }
        this.$message.success('添加分类成功!')
        this.gettableData()
        this.addDialogVisible = false
      })
    },
    async showeditDialog(id) {
      const { data: res } = await this.$http.get('categories/' + id)
      if (res.meta.status !== 200) {
        return this.$message.error('获取分类失败!')
      }
      this.editForm = res.data
      this.editDialogVisible = true
    },
    editSubmitBtn() {
      this.$refs.editFormRef.validate(async valid => {
        if (!valid) return
        const { data: res } = await this.$http.put('categories/' + this.editForm.cat_id, {
          cat_name: this.editForm.cat_name
        })
        if (res.meta.status !== 200) {
          return this.$message.error('更新分类信息失败!')
        }
        this.$message.success('更新分类信息成功!')
        this.gettableData()
        this.editDialogVisible = false
      })
    }
  },
  computed: {}
}
</script>

<style lang="less" scoped>
.zk-table {
  margin: 15px 0;
}
.igreen {
  color: green;
}
.ired {
  color: red;
}
.el-cascader {
  width: 100%;
}
</style>
